# 此文件是用来比较两个命令文件不同之处，并生产Ruby格式命令

#module Aio::Base::Toolkit
	class Diff
		
		# 输入文件1
		attr_accessor :input_1

		# 输入文件2
		attr_accessor :input_2

		def initialize(input_1, input_2)
			self.input_1 = File.open(input_1)
			self.input_2 = File.open(input_2)
			@context_name = "context"
		end

		def diff
			in_1 = @input_1.dup
			in_2 = @input_2.dup.readlines
			# 行计数从1开始
			line_count = 0
			res_text = []

			in_1.each_line do |line_1|
				# 加入了区间，如果不是在这个区间内，则忽略
				line_count += 1
				unless @range.nil?
					if ! @range.include?(line_count)
						next
					end
				end

				line_1 = line_1.chomp
				begin
					line_2 = in_2[line_count - 1].chomp
				rescue EOFError
					break
				end
				#new_line = "#{@context_name}[#{line_count}].match_block(/"
				new_line = "#{@context_name}.readline_match_block(/"
				
				# 按照单词进行比对
				word_1_arr = line_1.split(' ')
				word_2_arr = line_2.split(' ')

				word_arr = []
				merge_count = []
				word_1_arr.each_with_index do |word_1, i|

					word_2 = word_2_arr[i]
					
					if word_1 == word_2
						word_arr << inn(word_1)
					else
						word_arr << '(.*)'
						merge_count << i
					end
				end
				# 合并word_arr中相邻的(.*)
				real_word_arr = []
				word_arr.each_with_index do |word, i|
					if word != '(.*)'
						real_word_arr << word
						next
					end
					if word_arr[i+1] == '(.*)'
						next
					end
						# 加入(.*)
						real_word_arr << word
				end

				new_line << real_word_arr.join(' ')
				new_line << '/)'

				res_text << new_line
			end

			res = res_text.join("\n")
			puts res
		end

		def set_range(first, last)
			@range = Range.new(first, last)
		end

		def context_name=(var)
			@context_name = var
		end

		# 无害化处理
		def inn(word)
			word.gsub!('/', '\/')
			word.gsub!('(', '\(')
			word.gsub!(')', '\)')
			word.gsub!('.', '\.')
			word
		end
	end
#end

				
			
